/**
 *  Date           	Name    		     Change
 *  24/09/2014     	Roy Chan			 Creation
 *
 */
package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import persistence.model.File;

/**
 * @author Roy Chan
 *
 */
public class FileDaoImpl extends DaoBaseClass {

	private static final long serialVersionUID = 1L;
	PreparedStatement preparedStatement;
	Statement st;
	public FileDaoImpl(){
	}
	
	
	public void insert(File file){
		String insertTableSQL = "INSERT INTO file"
				+ "(file_id, file_size, file_name, file_address) VALUES"
				+ "(?,?,?,?)";
 
		try {
		preparedStatement = this.getConnection().prepareStatement(insertTableSQL);
		
		preparedStatement.setLong(1, file.getFile_id());
		preparedStatement.setLong(2, file.getFile_size());
		preparedStatement.setString(3, file.getFile_name());
		preparedStatement.setString(4, file.getFile_address());
		
		// execute insert SQL stetement
		preparedStatement.executeUpdate();
		
		//System.out.println("Record is inserted into table!");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ResultSet selectAll() {
		ResultSet rt = null;
		try {
			st = this.getConnection().createStatement();
			String sql = "SELECT * FROM file";
			rt= st.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rt;
	}
	
	/**
	 * delete all file records
	 */
	public void deleteAll(){
		
		StringBuffer sql = new StringBuffer();
		sql.append("DELETE * FROM file");
		
		Statement st;
		try {
			st = this.getConnection().createStatement();
			st.executeUpdate(sql.toString());
			st.executeUpdate(sql.toString());
			System.out.println("deleted");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
